﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using CMS.Core.Types;

namespace CMS.Core
{
    public class AdministratorManager : BaseManager<Administrator>
    {
        /// <summary>
        /// 账号是否存在
        /// </summary>
        /// <param name="accounts">账号</param>
        /// <returns></returns>
       public bool HasAccounts(string accounts)
        {
            return base.Repository.IsContains(a => a.Accounts == accounts);
        }


        /// <summary>
        /// 添加管理员账号
        /// </summary>
        /// <param name="entity">管理员账号实体</param>
        /// <returns></returns>
       public override Response Add(Administrator entity)
       {
           Response response = new Response();
           
           if(HasAccounts(entity.Accounts))
           {
               response.Code = 0;
               response.Message = "账号已经存在";
           }
           else
           {
               response = base.Add(entity);
           }
           return response;
       }

        /// <summary>
        /// 查找账号
        /// </summary>
        /// <param name="accounts">管理员账号</param>
        /// <returns></returns>
       public Administrator Find(string accounts)
       {
           return base.Repository.Find(a => a.Accounts == accounts);
       }
       
        /// <summary>
        /// 更新账号信息
        /// </summary>
        /// <param name="ID">管理员ID</param>
        /// <param name="ip">登陆IP</param>
        /// <param name="loginTime">登陆时间</param>
        /// <returns>响应信息</returns>
       public Response UpdateLoginInfo(int ID, string ip, DateTime loginTime)
       {
           Response response = new Response();
           Administrator admin = base.Find(ID);

           if(admin == null)
           {
               response.Code = 0;
               response.Message = "账号不存在";
           }
           else
           {
               admin.LoginIP = ip;
               admin.LoginTime = loginTime;

               response = base.Update(admin);
           }

           return response;
       }

        /// <summary>
        /// 验证
        /// </summary>
        /// <param name="accounts">账号</param>
        /// <param name="password">密码【密文】</param>
        /// <returns>0--账号不存在, 1--验证通过, 2--密码错误</returns>
       public Response Verify(string accounts, string password)
       {
           
           Response response = new Response();
           Administrator admin = base.Repository.Find(a => a.Accounts == accounts);

           if(admin == null)
           {
               response.Code = 0;
               response.Message = "账号不存在";
           }
           else if(admin.Password == password)
           {
               response.Code = 1;
               response.Message = "验证通过";
           }
           else
           {
               response.Code = 2;
               response.Message = "密码错误";
           }

           return response;
       }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="ID">账号ID</param>
        /// <param name="oldPassword">旧密码【密文】</param>
        /// <param name="newPassword">新密码【密文】</param>
        /// <returns>0 --账号不存在， 1 --修改成功</returns>
       public Response ChangePassword(int ID, string newPassword)
       {
            Response response = new Response();
            Administrator admin = base.Find(ID);

            if(admin == null)
            {
                response.Code = 0;
                response.Message = "账号不存在";
            }
            else
            {
                admin.Password = newPassword;
                response = base.Update(admin);
            }

            return response;
       }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ID">主键</param>
        /// <returns></returns>
       public override Response Delete(int ID)
       {
           Response response = new Response();
           
           if(Count() == 1)
           {
               response.Code = 0;
               response.Message = "管理员账号只有1个时不能删除";
           }
           else
           {
               var admin = base.Find(ID);
               response = base.Delete(admin.AdministratorID);
           }

           return response;
       }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="administratorIDList">删除实体的ID列表</param>
        /// <returns>0--删除失败, 1--删除成功， 2--部分删除</returns>
       public Response Delete(List<int> administratorIDList)
       {
           Response response = new Response();
           var totalAdmin = Count();
           var totalDel = administratorIDList.Count;

           foreach(var i in administratorIDList)
           {
               if(totalAdmin > 1)
               {
                   base.Repository.Delete(new Administrator() { AdministratorID = i }, false);
                   totalAdmin--;
               }
               else
               {
                   response.Message = "至少保留一个管理员";
               }
           }

           response.Data = base.Repository.Save();

           if(response.Data == totalDel)
           {
               response.Code = 1;
               response.Message = "成功删除" + response.Data + "个账号";
           }
           else if(response.Data > 0)
           {
               response.Code = 2;
               response.Message = "部分删除" + response.Data + "个账号";
           }
           else
           {
               response.Message = "删除失败";
               response.Code = 0;
           }

           return response;
       }


    }
}
